Imports System
Imports NUnit.Framework
Imports ValidationFramework
Imports ValidationFramework.Reflection

Public Class AddCustomRuleWithTypeCacheExample

#Region "Example"
    Public Class Person
        Private _name As String
        Private _validationManager As MemberValidationManager


        Public Sub New()
            _validationManager = ValidationManagerFactory.GetPropertyValidationManager(Me)
        End Sub



        Public Shared Sub CheckIsNotEmpty(ByVal sender As Object, _
        ByVal e As CustomValidationEventArgs)
            Dim name As String = CStr(e.TargetMemberValue)
            If String.IsNullOrEmpty(name) Then
                e.IsValid = False
            Else
                e.IsValid = True
            End If
        End Sub


        Public Function Validate() As Boolean
            _validationManager.Validate("Name")
            Return _validationManager.IsValid
        End Function


        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property


    End Class

    <TestFixture()> _
    Public Class ExampleUsage
        <Test()> _
        Public Sub Run()
            Dim personType As Type = GetType(Person)
            Dim propertyDescriptor As PropertyDescriptor = _
            TypeCache.GetType(personType.TypeHandle).GetOrCreatePropertyDescriptor("Name")
            Dim rule As New CustomRule("Name cannot be empty.", False, _
            New EventHandler(Of CustomValidationEventArgs)(AddressOf Person.CheckIsNotEmpty) _
            , "Name cannot be empty.")
            propertyDescriptor.Rules.Add(rule, Nothing)
            Dim _person As New Person
            Assert.IsFalse(_person.Validate)
            _person.Name = "John"
            Assert.IsTrue(_person.Validate)
        End Sub

    End Class
#End Region
    End Class








